// pages/seat_lib/seat_lib.js
const baseURL = 'http://100.2.178.56:8080'
import {
    formatDate,
    getDates
} from "util.js";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 0,
        reserveTimes: null,
        reserveTimes_index: 0,
        reserveTimes_range_index: 0,
        reserveTimes_range: null,
        reserveTimes_index_offset: 0,
        reservaAble: true,

        // reserve_state:[{
        //     res_sta_id:null,
        //     comment:null
        // }],
        // sjxx: [
        //     ["时间段1:8:00-9:30", 0],
        //     ["时间段2:9:30-11:00", 1],
        //     ["时间段3:11:00-12:30", 2],
        //     ["时间段4:12:30-2:00", 3],
        //     ["时间段5:2:00-4:00", 4]
        // ],
        // sjxxindex: 0,
        floorList: [{
            floId: 0,
            floNum: 0
        }],
        floNums: '',
        reserveStates: null,
        reserveStates_index: 0,
        reserveState_range: null,
        // ztarr: [
        //     ['查看全部', 0],
        //     ['已预约', 1],
        //     ['可预约', 2]
        // ],
        // yyzt: 0,
        rmb: '',
        seaId: '',
        seatStates: null,
        tomorrow: false,
    },

    getReserveTime: function () {

    },
    showxq: function (e) {

        var obj = {
            'seat': e.currentTarget.dataset.value,
            'seaStaId': e.currentTarget.dataset.name.seatState.seaStaId,
            'floNum': e.currentTarget.dataset.id,
            'reserveState': this.data.reserveStates[this.data.reserveStates_index],
            'reserveTime': this.data.reserveTimes[this.data.reserveTimes_index],
            'tomorrow': this.data.tomorrow,
            'resTime': e.currentTarget.dataset.name.timeList,
            // 'resEndTime':e.currentTarget.dataset.name.resEndTime,
        }
        if (getApp().globalUserInfo == null) {
            // 教师用户
            var strs = JSON.stringify(obj)
            wx.navigateTo({
                url: '/pages/seat_lib/seat_detail_tea/seat_detail_tea?detail=' + strs,
            })
        } else {
            //学生用户
            var strs = JSON.stringify(obj)
            setTimeout(function () {
                wx.navigateTo({
                    url: '/pages/seat_lib/seat_detail_stu/seat_detail_stu?detail=' + strs,
                })
            }, 200)
        }


    },
    updateSeatState: function (resTimeId) {
        var that = this;
        /**
         * 获取所有座位状态
         */
        wx.request({
            url: baseURL + '/seatState/list',
            method: "GET",
            data: {
                "resTimeId": resTimeId
            },
            success: function (res) {
                var resData = res.data;
                console.log("updateSeatState=> resData.data.seatStatelist"+  resData.data.seatStatelist)
                if (resData.code == 0) {
                    that.setData({
                        seatStates: resData.data.seatStatelist
                    });
                } else {
                    wx.showToast({
                        title: resData.message,
                        duration: 2000
                    })
                }
            }
        })
    },
    /**
     * 显示选中的时间
     * @param {*} e 
     */
    bindPickerChangesReserveTime: function (e) {
        var that = this
        // console.log(e.detail.value)
        this.setData({
            reserveTimes_range_index: e.detail.value
        })
        // console.log("reserveTimes_index:::::"+this.data.reserveTimes_index)
        var a = this.data.reserveTimes_index_offset + parseInt(e.detail.value);
        if (a > this.data.reserveTimes_range.length - 1) {
            this.setData({
                reserveTimes_index: (a % (this.data.reserveTimes_range.length - 1)) - 1
            })
        } else {
            this.setData({

                reserveTimes_index: a
            })
        }

        console.log("e.detail.value")
        console.log(e.detail.value);
        if (e.detail.value == 1) {
            this.setData({
                tomorrow: true
            })
        } else {
            this.setData({
                tomorrow: false
            })
        }

        if (e.detail.value > 1) {
            this.setData({
                reservaAble: false
            })
        } else {
            this.setData({
                reservaAble: true
            })
        }
        console.log(this.data.reserveTimes[this.data.reserveTimes_index])
        console.log("that.data.reserveTimes_index")
        console.log(that.data.reserveTimes_index)
        this.updateSeatState(this.data.reserveTimes[this.data.reserveTimes_index].resTimeId);

        // console.log("e.detail.value:::::"+e.detail.value)
        // console.log("reserveTimes_index:::::"+this.data.reserveTimes_index)
    },
    /**
     * 显示选中的楼层
     * @param {}} e 
     */
    bindPickerChangeplace: function (e) {
        var that = this
        console.log("that.data.reserveTimes_index")
        console.log(that.data.reserveTimes_index)
        this.updateSeatState(this.data.reserveTimes[this.data.reserveTimes_index].resTimeId);
        var that = this;
        this.setData({
            index: e.detail.value
        });
        if (e.detail.value > 0) {
            wx.request({
                url: baseURL + '/seat/floor/list',
                method: "GET",
                data: {
                    'floId': that.data.floorList[e.detail.value - 1].floId
                },
                success: function (res) {
                    var resData = res.data;
                    if (resData.code == 0) {
                        that.setData({
                            rmb: resData.data.seatList
                        });
                    } else {
                        wx.showToast({
                            title: resData.message,
                            duration: 2000
                        })
                    }
                }
            })

        } else {
            /**
             * 获取所有对应楼层的座位信息
             */
            wx.request({
                url: baseURL + '/seat/floor/list',
                method: "GET",
                data: {
                    'floId': 0
                },
                success: function (res) {
                    var resData = res.data;
                    if (resData.code == 0) {
                        console.log("seatList：" + resData.data.seatList);
                        that.setData({
                            rmb: resData.data.seatList
                        });
                    } else {
                        console.log("seatList：" + resData.message);
                        wx.showToast({

                            title: resData.message,
                            duration: 2000
                        })
                    }
                }
            })
        }

    },
    /**
     * 显示选中的状态
     * @param {*} e 
     */
    bindPickerChangezt: function (e) {
        var that = this
        console.log("that.data.reserveTimes_index")
        console.log(that.data.reserveTimes_index)
        this.updateSeatState(this.data.reserveTimes[this.data.reserveTimes_index].resTimeId);
        var that = this

        this.setData({
            reserveStates_index: e.detail.value
        });
        console.log(e.detail.value);

        // wx.request({
        //     url: 'http://localhost:8080/seat/state/list',
        //     method: "GET",
        //     data: {
        //         'seaRevState': e.detail.value
        //     },
        //     success: function (res) {
        //         var resData = res.data;
        //         if (resData.code == 0) {
        //             that.setData({
        //                 rmb: resData.data.seatList
        //             });
        //         } else {
        //             wx.showToast({
        //                 title: resData.message,
        //                 duration: 2000
        //             })
        //         }
        //     }
        // })



    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        var that = this
        /**
         * 获取所有预约状态
         */
        wx.request({
                url: baseURL + '/reserveState/list',
                method: "GET",
                success: function (res) {
                    var resData = res.data;
                    if (resData.code == 0) {
                        that.setData({
                            reserveStates: resData.data.reserveStates
                        });
                        let array = new Array(that.data.reserveStates.length);
                        for (var i = 0; i < that.data.reserveStates.length; i++) {
                            array[i] = that.data.reserveStates[i].comment
                        }
                        that.setData({
                            reserveState_range: array
                        })
                    } else {
                        wx.showToast({
                            title: resData.message,
                            duration: 2000
                        })
                    }
                }
            }),

            /**
             * 获取所有座位
             */
            wx.request({
                url: baseURL + '/seat/list',
                method: "GET",
                success: function (res) {
                    var resData = res.data;
                    // console.log(",,,,,"+ resData.data.seatList[0].seaId)
                    if (resData.code == 0) {
                        that.setData({
                            rmb: resData.data.seatList
                        });
                    } else {
                        wx.showToast({
                            title: resData.message,
                            duration: 2000
                        })
                    }
                }
            }),

            /**
             * 获取所有楼层
             */
            wx.request({
                url: baseURL + '/floor/list',
                method: "GET",
                success: function (res) {
                    var resData = res.data;
                    var floors = resData.data.floorList;
                    // console.log(floors[0].floNum)
                    if (resData.code == 0) {
                        that.setData({
                            floorList: resData.data.floorList
                        });
                        let array = new Array(that.data.floorList.length + 1);
                        array[0] = '全部位置';
                        for (var i = 0; i < that.data.floorList.length; i++) {
                            if (that.data.floorList[i].floNum > 0) {
                                array[i + 1] = that.data.floorList[i].floNum + '楼'
                            }

                        }

                        that.setData({
                            'floorNums': array
                        });
                        console.log(that.data.floorList[0].floNum);
                    } else {
                        wx.showToast({
                            title: resData.message,
                            duration: 2000
                        })
                    }
                }
            })


        /**
         * 获取所有可预约时间
         */
        wx.request({
            url: baseURL + '/reserveTime/list',
            method: "GET",
            success: function (res) {
                var resData = res.data;
                if (resData.code == 0) {
                    that.setData({
                        reserveTimes: resData.data.reserveTimes
                    });
                    /**
                     * 引用工具类
                     * 获取当前周几
                     */
                    let time = formatDate(new Date());
                    let date = getDates(7, time);

                    that.setData({

                        reserveTimes_index: date[0].week - 1,
                        reserveTimes_index_offset: date[0].week - 1
                    })
                    /**
                     * 限制显示的时间
                     */
                    let array = new Array(that.data.reserveTimes.length);
                    let array1 = new Array(that.data.reserveTimes.length);
                    var j = 0;
                    for (var i = 0; i < that.data.reserveTimes.length; i++) {
                        if (that.data.reserveTimes[i].timeWeekday < date[0].week) {
                            array1[i] = that.changeReserveTime(that.data.reserveTimes[i])
                            j++;
                        } else {
                            array[i - j] = that.changeReserveTime(that.data.reserveTimes[i])
                        }
                    }
                    var k = that.data.reserveTimes.length;
                    for (var i = 0; i < j; i++) {
                        array[k - j] = array1[i];
                        k++;
                    }
                    that.setData({
                        reserveTimes_range: array
                    })
                    console.log("that.data.reserveTimes_index")
                    console.log(that.data.reserveTimes_index)
                    that.updateSeatState(that.data.reserveTimes[that.data.reserveTimes_index].resTimeId);
                } else {
                    wx.showToast({
                        title: resData.message,
                        duration: 2000
                    })
                }


            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    changeReserveTime: function (reserveTime) {
        var arrayString;
        switch (reserveTime.timeWeekday) {
            case 1: {
                arrayString = '周一,' + reserveTime.timeStartTime + ':00~' + reserveTime.timeEndTime + ':00 ';
                break;
            }
            case 2: {
                arrayString = '周二,' + reserveTime.timeStartTime + ':00~' + reserveTime.timeEndTime + ':00 ';
                break;
            }
            case 3: {
                arrayString = '周三,' + reserveTime.timeStartTime + ':00~' + reserveTime.timeEndTime + ':00 ';
                break;
            }
            case 4: {
                arrayString = '周四,' + reserveTime.timeStartTime + ':00~' + reserveTime.timeEndTime + ':00 ';
                break;
            }
            case 5: {
                arrayString = '周五,' + reserveTime.timeStartTime + ':00~' + reserveTime.timeEndTime + ':00 ';
                break;
            }
            case 6: {
                arrayString = '周六,' + reserveTime.timeStartTime + ':00~' + reserveTime.timeEndTime + ':00 ';
                break;
            }
            case 7: {
                arrayString = '周日,' + reserveTime.timeStartTime + ':00~' + reserveTime.timeEndTime + ':00 ';
                break;
            }
        }
        return arrayString;
    }

})